package datalookin.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.whu.zj.R;
import com.whu.zj.UIHandler;

import java.util.ArrayList;
import java.util.List;

import datalookin.adapter.AllPlantMonitorAdapter;
import datalookin.dto.WaterPlantMainDataDto;
import datalookin.network.AllWaterPlantRealData;
import inter.Delegate;

/**
 * Created by Administrator on 2016/3/12.
 */
public class PlantMonitorActivity extends UIHandler.BaseActivity {

    List<WaterPlantMainDataDto> waterPlantMainDataDtos = new ArrayList<WaterPlantMainDataDto>();

    ListView lv_real;
    AllPlantMonitorAdapter allPlantMonitorAdapter = new AllPlantMonitorAdapter();
    AllWaterPlantRealData allWaterPlantRealData;

    Button btn_line;

    View v_history;
    private Button btn_back;
    SwipeRefreshLayout refreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allplantmonitor);
        initview();

        initdata();
    }

    private void initdata() {
        allWaterPlantRealData = new AllWaterPlantRealData();
        allWaterPlantRealData.setDelegate(allWaterPlantMainData);
        allWaterPlantRealData.get();


    }

    private void initview() {
        allPlantMonitorAdapter.setWaterPlantMainDataDtos(waterPlantMainDataDtos);
        lv_real = (ListView) findViewById(R.id.id_lv_historyplant);

        lv_real.setEmptyView(findViewById(R.id.id_v_nonemsg));
        lv_real.setAdapter(allPlantMonitorAdapter);

        btn_line = (Button) findViewById(R.id.id_btn_line);
        btn_line.setOnClickListener(onClickListener);

        v_history = findViewById(R.id.id_bottom_history);
        v_history.setOnClickListener(onClickListener);

        btn_back = (Button) findViewById(R.id.id_btn_back);
        btn_back.setOnClickListener(onClickListener);

        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.id_refresh);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initdata();
            }
        });
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn_line)
            {
                Intent intent = new Intent(PlantMonitorActivity.this,GuanWangMonitorActivity.class);
                PlantMonitorActivity.this.startActivity(intent);

            }

            else if (v == v_history)
            {
                Intent intent = new Intent(PlantMonitorActivity.this,HistoryPlantMonitorActivity.class);
                PlantMonitorActivity.this.startActivity(intent);
            }

            else if (v == btn_back)
            {
                PlantMonitorActivity.this.finish();
            }
        }
    };


    Delegate allWaterPlantMainData = new Delegate() {
        @Override
        public void onResponse(String response) {
            if (refreshLayout.isRefreshing())
                refreshLayout.setRefreshing(false);

            if (!response.startsWith("[")) return;
            List<WaterPlantMainDataDto> waterPlantMainTmp = JSON.parseArray(response,WaterPlantMainDataDto.class);
            waterPlantMainDataDtos.clear();
            waterPlantMainDataDtos.addAll(waterPlantMainTmp);
            allPlantMonitorAdapter.notifyDataSetChanged();
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            if (refreshLayout.isRefreshing())
                refreshLayout.setRefreshing(false);
            Toast.makeText(PlantMonitorActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (allWaterPlantRealData != null)
            allWaterPlantRealData.cancel();
    }
}
